#include "stm32f10x.h"                  // Device header

int KeyNum = 0;
void Exit_Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource11);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource15);

	
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line11|EXTI_Line12| EXTI_Line13 |EXTI_Line14| EXTI_Line15; 
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStructure);
	


	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_Init(&NVIC_InitStructure);

}
void EXTI15_10_IRQHandler(void)
{
	
	if (EXTI_GetITStatus(EXTI_Line12) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0)
		{
			KeyNum = 1;
			while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)==0);
		}
		EXTI_ClearITPendingBit(EXTI_Line12);
	}
	
	if (EXTI_GetITStatus(EXTI_Line13) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) == 0)
		{
			KeyNum = 9;
		}
		EXTI_ClearITPendingBit(EXTI_Line13);
	}
	
	if (EXTI_GetITStatus(EXTI_Line14) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
		{
			KeyNum = 2;
			while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14)==0);
		}
		EXTI_ClearITPendingBit(EXTI_Line14);
	}
	
	if (EXTI_GetITStatus(EXTI_Line15) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == 0)
		{
			KeyNum = 3;
			while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)==0);			
		}
		EXTI_ClearITPendingBit(EXTI_Line15);
	}
	if (EXTI_GetITStatus(EXTI_Line11) == SET)
	{
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
		{
			KeyNum = 4;
			while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)==0);
		}
		EXTI_ClearITPendingBit(EXTI_Line11);
	}
}

